import sys
import os
import hashlib

def hashsum(file, algorithm='all', chunk_size=65535):
    def _core(file, algorithm, chunk_size):
        if algorithm.lower() == 'md5':
            hash = hashlib.md5()
        elif algorithm.lower() == 'sha1':
            hash = hashlib.sha1()
        elif algorithm.lower() == 'sha256':
            hash = hashlib.sha256()
        else:
            raise ValueError('Unsupported hash algorithm. Use md5, sha1, or sha256')
        
        with open(file, 'rb') as f:
            while chunk := f.read(chunk_size):
                hash.update(chunk)
        return hash.hexdigest()
    
    if algorithm.lower() == 'all' or algorithm is None:
        return {k: _core(file, k, chunk_size) for k in ('md5', 'sha1', 'sha256')}
    else:
        return _core(file, algorithm, chunk_size)

def main():
    if len(sys.argv) < 3:
        print("Usage: hashsum.py <file_path> <algorithm>")
        print("Algorithm options: md5, sha1, sha256, all")
        sys.exit(1)
    
    file_path = sys.argv[1]
    algorithm = sys.argv[2]
    
    if not os.path.exists(file_path):
        print(f"Error: File '{file_path}' does not exist.")
        sys.exit(1)
    
    try:
        result = hashsum(file_path, algorithm)
        if isinstance(result, dict):
            print("Hash results:")
            for algo, hash_value in result.items():
                print(f"{algo.upper()}: {hash_value}")
        else:
            print(f"{algorithm.upper()}: {result}")
    except ValueError as e:
        print(f"Error: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()
